<template>
  <div class="auth-container">
    <!-- 左侧表单区 -->
    <div class="form-section">
      <div class="form-wrapper">
        <h2>用户注册</h2>
        <form @submit.prevent="handleRegister">
          <div class="form-group">
            <label for="register-username">用户名：</label>
            <input type="text" id="register-username" v-model="register.username" placeholder="请输入用户名" required>
          </div>
          <div class="form-group">
            <label for="register-password">密码：</label>
            <input type="password" v-model="register.password" placeholder="请输入密码" required>
          </div>
          <div class="form-group">
            <label for="register-confirmPassword">再次输入密码：</label>
            <input type="password" v-model="register.confirmPassword" placeholder="请确认密码" required>
          </div>
          <div class="form-group">
            <label for="register-identity">身份：</label>
            <select id="register-identity" v-model="register.identity">
              <option value="patient">普通用户</option>
              <option value="manager">管理人员</option>
            </select>
          </div>

          <div class="form-group">
            <label for="register-phone">手机号：</label>
            <input type="text" v-model="register.phone" placeholder="请输入手机号" required>
          </div>
          <div class="form-group">
            <div class="code-group">
              <label for="register-code">验证码：</label>
              <input type="text" v-model="register.code" placeholder="请输入验证码" required>
              <button type="button" class="code-btn" @click="sendCode" :disabled="isSending">
                {{ isSending ? `${countdown}s后重试` : '获取验证码' }}
              </button>
            </div>
            <div v-if="receivedCode" class="code-reminder">
              验证码: {{ receivedCode }}
            </div>
          </div>
          <div class="form-group">
            <label for="register-gender">性别：</label>
            <div class="gender-options-container">
              <div class="gender-options">
                <label class="gender-option">
                  <input type="radio" id="register-gender-male" name="register-gender" value="male" v-model="register.gender" required>
                  <span>男</span>
                </label>
                <label class="gender-option">
                  <input type="radio" id="register-gender-female" name="register-gender" value="female" v-model="register.gender" required>
                  <span>女</span>
                </label>
              </div>
            </div>
          </div>
          <div class="form-group">
            <label for="register-age">年龄：</label>
            <input type="number" id="register-age" v-model="register.age" placeholder="请输入年龄">
          </div>
          <div class="form-group condition-group">
              <div class="condition-label">
                <span class="main-text" for="register-condition">病情信息：</span>
                <span class="sub-text">（病友填写）</span>
              </div>
              <textarea id="register-condition" v-model="register.condition" placeholder="请简要描述病情"></textarea>
          </div>
          <button type="submit" class="register-btn">注 册</button>
        </form>
        <div class="footer-links">
          已有账号？<a @click="switchToLogin">立即登录</a>
        </div>
      </div>
    </div>

    <!-- 右侧展示区 -->
    <div class="display-section">
      <div class="form-wrapper">
        <div class="logo-container">
          <h3>加入“愈见希望”病友交流平台</h3>
          <p>开启您的健康交流之旅</p>
          <img src="src\assets\images\logo.png" alt="网站Logo">
        </div>
        <div class="agreement">
          <p>注册即表示您同意我们的</p>
          <a href="./UserAgreement" target="_blank">《用户协议》</a>和
          <a href="./Privacy" target="_blank">《隐私政策》</a><br>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      register: {
        phone: '',
        code: '',
        password: '',
        confirmPassword: '',
        identity: 'patient',
        username: '',
        gender: 'male',
        age: '',
        condition: ''
      },
      isSending: false,
      countdown: 60,
      errorMessage: '',
      receivedCode: '' // 用于显示验证码
    }
  },
  methods: {
    async handleRegister() {
    this.errorMessage = ''; // 清空错误信息

    // 验证密码是否一致
    if (this.register.password !== this.register.confirmPassword) {
        this.errorMessage = '两次输入的密码不一致';
        return;
    }

    try {
        const response = await axios.post('http://localhost:8081/api/auth/register', {
            username: this.register.username,
            password: this.register.password,
            confirmPassword: this.register.confirmPassword,
            identity: this.register.identity,
            phone: this.register.phone,
            code: this.register.code,
            gender: this.register.gender,
            age: this.register.age ? parseInt(this.register.age) : null,
            condition: this.register.condition
        }, {
            withCredentials: true // 确保携带凭证
        });

        if (response.data.success) {
            setTimeout(() => {
                this.$router.push('/');
            }, 2000);
        } else {
            this.errorMessage = response.data.message || '注册失败，请检查输入信息';
            console.error('注册失败:', response.data.message || '未知错误');
        }
    } catch (error) {
        if (error.response) {
            this.errorMessage = error.response.data.message || '服务器返回错误';
            console.error('注册失败:', error.response.data);
        } else {
            this.errorMessage = '网络错误，请稍后再试';
            console.error('注册失败:', error);
        }
    }
},
    async sendCode() {
      try {
        const response = await axios.get(`http://localhost:8081/api/auth/sendCode?phone=${this.register.phone}`);
        this.receivedCode = response.data.code; // 使用后端返回的验证码
        this.isSending = true;
        const timer = setInterval(() => {
          this.countdown--;
          if (this.countdown <= 0) {
            clearInterval(timer);
            this.isSending = false;
            this.countdown = 60;
          }
        }, 1000);
      } catch (error) {
        console.error('获取验证码失败:', error);
      }
    },
    switchToLogin() {
      this.$router.push('/');
    }
  }
};
</script>


<style scoped>
.auth-container {
  width:1000px;
  display: flex;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f9fc 0%, #e6f0fa 100%);
  margin: 0 auto;
  position: relative;
  padding: 0 50px; 
}
.form-section, .display-section {
  flex: 1;
  padding: 40px 0; /* 上下内边距 */
}
.auth-container::before {
  content: '';
  position: absolute;
  top: -50px;
  right: -50px;
  width: 200px;
  height: 200px;
  background: rgba(64, 158, 255, 0.08);
  border-radius: 50%;
}

.auth-container::after {
  content: '';
  position: absolute;
  bottom: -30px;
  left: -30px;
  width: 150px;
  height: 150px;
  background: rgba(64, 158, 255, 0.05);
  border-radius: 50%;
}
.form-section {
  flex: 0 0 480px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  position: relative;
  overflow: hidden;
}


.form-wrapper {
  width: 100%;
  max-width: 420px; /* 稍微增加最大宽度 */
  padding: 2rem 2.5rem; /* 增加左右内边距 */
}

.form-wrapper:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
  font-size: 24px;
  font-weight: 500;
}
.center-label{
  display: block;
  text-align: center;
  margin-bottom: 10px;
}
.form-group {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}
.form-group label {
  width: 100px; /* 固定标签宽度 */
  text-align: right;
  margin-right: 15px;
  flex-shrink: 0;
}
.form-group input,
.form-group select,
.form-group textarea  {
  flex: 1; /* 占据剩余空间 */
  max-width: 250px; /* 限制输入框最大宽度 */
  padding: 12px 16px;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  font-size: 14px;
  transition: all 0.3s;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus{
  border-color: #409eff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
  outline: none;
}
.form-group label{
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  color: #5e6d82;
}
.form-group select {
  padding: 12px 15px;
  font-size: 14px;
  line-height: 1.5;
  color: #495057;
  background-color: #fff;
  background-clip: padding-box;
  border: 1px solid #ced4da;
  border-radius: 4px;
  transition: border-color 0.15s ease-in-out, 
              box-shadow 0.15s ease-in-out;
}
.condition-group {
  display: flex;
  align-items: flex-start;
  margin-bottom: 20px;
}

.condition-label {
  width: 100px; /* 与其他label同宽 */
  text-align: right;
  margin-right: 15px;
  flex-shrink: 0;
}

.main-text {
  display: block;
  font-size: 14px;
  color: #5e6d82;
}

.sub-text {
  display: block;
  font-size: 12px;
  color: #909399;

  margin-top: 4px;
}

#register-condition {
  flex: 1;
  max-width: 250px;
  height: 80px;
  padding: 12px 16px;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
}
.gender-container {
    display: flex;
    justify-content: center; 
    align-items: center;
    margin:10px 0;
}

.gender-container label[for="register-gender"] {
    font-size: 16px;
    color: #5e6d82;
    font-weight: 500;
    margin-right: 10px;
}
.gender-options-container {
  display: flex;
  justify-content: center;
  width: 100%;
}

.gender-options {
  display: flex;
  gap: 30px;
  justify-content: center; /* 确保选项居中 */
}

.gender-option {
  display: flex;
  align-items: center;
  cursor: pointer;
}


.gender-option input[type="radio"] {
  width: 16px;
  height: 16px;
  border: 2px solid #ddd;
  border-radius: 50%;
  margin-right: 8px;
  position: relative;
  outline: none;
}

.gender-option span {
    font-size: 14px;
    color: #555;
    transition: color 0.3s ease-in-out;
}

.gender-option input[type="radio"]:checked {
  border-color: #409EFF;
  background-color: #409EFF;
}
.gender-option:hover span {
    color: #007bff;
}
.radio-text {
  font-size: 14px;
  color: #333;
}
/* 右侧展示区优化 */
.display-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  position: relative;
  overflow: hidden;
}

.form-wrapper {
  width: 100%;
  max-width: 400px;
  background: white;
  border-radius: 16px;
  padding: 2.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 1;
  transition: transform 0.3s, box-shadow 0.3s;
}

.form-wrapper:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.1);
}

.logo-container {
  text-align: center;
  margin-bottom: 2rem;
}

.logo-container img {
    width: 300px;
    height: 180px;
  }
  
.logo-container h3 {
  font-size: 1.8rem;
  color: #2c3e50;
  font-weight: 600;
  margin-bottom: 0.8rem;
  position: relative;
  display: inline-block;
}

.logo-container h3::after {
  content: '';
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 50px;
  height: 3px;
  background: linear-gradient(to right, #4facfe, #00f2fe);
}

.logo-container p {
  color: #718096;
  font-size: 1rem;
  line-height: 1.6;
  max-width: 80%;
  margin: 0 auto 1.5rem;
}

/* 协议链接优化 */
.agreement {
  margin-top: 2rem;
  text-align: center;
  font-size: 0.9rem;
  color: #718096;
  line-height: 1.6;
  position: relative;
  padding-top: 1.5rem;
}

.agreement::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 80px;
  height: 1px;
  background: #e2e8f0;
}

.agreement a {
  color: #4facfe;
  font-weight: 500;
  text-decoration: none;
  transition: all 0.3s;
  position: relative;
}

.agreement a::after {
  content: '';
  position: absolute;
  bottom: -2px;
  left: 0;
  width: 0;
  height: 1px;
  background: #4facfe;
  transition: width 0.3s;
}

.agreement a:hover {
  color: #3a9cf7;
}

.agreement a:hover::after {
  width: 100%;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .display-section {
    padding: 1.5rem;
  }
  
  .logo-container img {
    width: 120px;
    height: 120px;
  }
  
  .logo-container h3 {
    font-size: 1.5rem;
  }
}
/* 按钮样式优化 */
.register-btn {
  width: 100%;
  padding: 12px;
  background: #409eff;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  margin-top: 10px;
}

.register-btn:hover {
  background: #66b1ff;
  box-shadow: 0 2px 8px rgba(103, 194, 58, 0.3);
}
.form-group:nth-of-type(5) {
  justify-content: flex-end;
}
.form-group:nth-of-type(3) {
  display: flex;
  align-items: center;
}

.form-group:nth-of-type(3) label {
  white-space: nowrap; /* 防止标签换行 */
}
.form-group:nth-of-type(9) {
  align-items: flex-start; /* 顶部对齐 */
}

.form-group:nth-of-type(9) textarea {
  height: 80px; /* 设置固定高度 */
  white-space: pre-wrap; /* 允许自动换行 */
}
.form-group:nth-of-type(5), /* 手机号 */
.form-group:nth-of-type(6) /* 验证码 */ {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.code-group {
  display: flex;
  align-items: center;
  width: 100%;
}


.code-group input {
  width: 150px; /* 固定验证码输入框宽度 */
  margin-right: 10px;
}

.code-btn {
  min-width: 80px;
  padding: 12px;
  background: #f5f7fa;
  border: 1px solid #dcdfe6;
  border-radius: 6px;
  color: #606266;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
  flex-shrink: 0; 
}

.code-btn:hover {
  background: #e6e9ed;
}

.code-btn:disabled {
  background: #f5f7fa;
  color: #c0c4cc;
  cursor: not-allowed;
}

/* 底部链接优化 */
.footer-links {
  margin-top: 24px;
  text-align: center;
  font-size: 14px;
  color: #909399;
}

.footer-links a {
  color: #409eff;
  cursor: pointer;
  text-decoration: none;
  transition: color 0.2s;
}

.footer-links a:hover {
  color: #66b1ff;
  text-decoration: underline;
}
.required-field::after {
  content: " *";
  color: red;
}
</style>